package com.hysteria.game;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.geom.Shape;

import com.hyteria.render.Renderable;
import com.hyteria.render.Renderer;



/*
 * 
 * 	Name: 		Entity
 *  Purpose: 	An entity describes an object that can be drawn and has an update loop. 
 * 
 */

public abstract class Entity extends Renderable implements Behavior {
	
	private static ArrayList<Entity> gameEntities = new ArrayList<Entity>();
	
	public static void register(Entity ent){
		gameEntities.add(ent);
		Game.registerBehavior(ent);  //This may not necessarily be here in the future as it may be attached/not attached under different circumstances
		Renderer.registerRenderable(ent); //This may not necessarily be here in the future i.e. once we make an algorithm that only registers items that NEED to be rendered
	}
	
	public static void remove(Entity ent){
		gameEntities.remove(ent);
	}
	
	public static ArrayList<Entity> getAll(){ return gameEntities; }
	
	private Cell inCell = null;
	
	public Entity(Shape shape){
		setShape(shape);
		register(this);
	}
	
	public void placeInCell(Cell c){
		if(inCell != c){
			inCell = c;
			c.putEntityInCell(this);
		}
		
		setX(inCell.getX() + Cell.cellSize/2 - (int)getWidth()/2);
		setY(inCell.getY() + Cell.cellSize/2 - (int)getHeight()/2);
	}
	
	public void placeInCell(int gX, int gY){
		Cell c = Game.getMap().getCell(gX, gY);
		if(c != null){
			placeInCell(c);
		}
	}
	
	public void notifyCellPlacement(Cell c){
		if(inCell != c){
			placeInCell(c);
		}
	}
	
	public void removeFromCell(){
		inCell = null;
	}
	
	public Cell getCell(){
		return inCell;
	}
}
